import api from 'api/api.js';
const  getJsApiSignature = async ()=> {
	let res = await api.getJsApiSignature({url: location.origin + location.pathname});
	if (res.code === '0000') {
		return res.result;
	}else{
		alert('获取微信分享签名失败')
	}
}

const shareJs = async () => {
    //  微信分享配置
	let { appId, timestamp, nonceStr, signature } = await getJsApiSignature();
    wx.config({
        debug: false,
        appId,
        timestamp: parseInt(timestamp),
        nonceStr,
        signature,
        jsApiList: [
            "checkJsApi",
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "onMenuShareQQ",
            "onMenuShareWeibo",
            "onMenuShareQZone",
        ]
    });

    //  接口检查列表
    wx.checkJsApi({
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone'], // 需要检测的JS接口列表
        success: function (res) {
            console.log(res);
        }
    });

    const share_content = {
        title: "测试标题",
        desc: "测试描述",
        link: 'http://app.chongdao.net/',
        imgUrl: 'http://app.chongdao.net/logo.jpg', //分享是封面图片，不能为空
        success: function() {
            alert('分享成功')
        },
        cancel: function() {
            alert('取消分享')
        }
    }

    wx.ready(() => {
        wx.onMenuShareTimeline(share_content);
        wx.onMenuShareAppMessage(share_content);
    });
}


export default shareJs
